package com.irdstudio.efp.nls.service.impl.ms;

import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.efp.nls.common.constant.MSRefuseMsgConstant;
import com.irdstudio.efp.nls.service.impl.SoltServiceImpl;
import com.irdstudio.efp.nls.service.vo.NlsApplyInfoVO;
import com.irdstudio.efp.nls.service.vo.queue.NlsProcessBizVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 需求编号：xxxxxx<br/>
 * 问题编号：<br/>
 * 功能描述：马上贷款审核拒绝通知插槽<br/>
 * 创建时间：2020/6/5 19:23
 * @Author tiankg
 * @Version 1.0
 * @Since 1.0
 */
@Service("msLoanApprRefuseNoticeService")
public class MsLoanApprRefuseNoticeServiceImpl extends SoltServiceImpl {
    private static Logger logger = LoggerFactory.getLogger(MsLoanApprNoticeServiceImpl.class);

//    @Autowired
//    @Qualifier("msLoanService")
//    private MsLoanService msLoanService;// 马上贷款审核通知

    @Override
    protected void doExecute(NlsProcessBizVO nlsProcessBizVo, NlsApplyInfoVO nlsApplyInfoVO) throws Exception {
        String applySeq = nlsProcessBizVo.getApplySeq();// 流水号

        try {
            logger.info("马上贷款审核通知接口（广州银行->马上），流水号:" + applySeq);
            Objects.requireNonNull(nlsApplyInfoVO);// 网贷申请信息不能为空
            /*
            try {
                MsLoanReq msLoanReq = new MsLoanReq();
                msLoanReq.setTxnFlowNo(applySeq);// 交易流水号
                msLoanReq.setAudRslt(MsLoanConstant.AudRsltEnum.REJECT.VALUE);
                if (nlsApplyInfoVO.getRefuseCause().contains(MSRefuseMsgConstant.MsRefuseMsgEnum.REASON_001.getCode()) || nlsApplyInfoVO.getRefuseCause().contains(MSRefuseMsgConstant.MsRefuseMsgEnum.REASON_002.getCode())
                            || nlsApplyInfoVO.getRefuseCause().contains(MSRefuseMsgConstant.MsRefuseMsgEnum.REASON_003.getCode()) || nlsApplyInfoVO.getRefuseCause().contains(MSRefuseMsgConstant.MsRefuseMsgEnum.REASON_004.getCode())
                            || nlsApplyInfoVO.getRefuseCause().contains(MSRefuseMsgConstant.MsRefuseMsgEnum.REASON_005.getCode()) || nlsApplyInfoVO.getRefuseCause().contains(MSRefuseMsgConstant.MsRefuseMsgEnum.REASON_006.getCode())
                            || nlsApplyInfoVO.getRefuseCause().contains(MSRefuseMsgConstant.MsRefuseMsgEnum.REASON_007.getCode())){
                        msLoanReq.setRfsRsn(MSRefuseMsgConstant.RESULT_001);
                }else if (nlsApplyInfoVO.getRefuseCause().contains(MSRefuseMsgConstant.MsRefuseMsgEnum.REASON_010.getCode())){
                        msLoanReq.setRfsRsn(MSRefuseMsgConstant.RESULT_002);
                }else {
                        msLoanReq.setRfsRsn(MSRefuseMsgConstant.RESULT_999);
                }

                MsLoanResp msLoanResp = msLoanService.msLoan(msLoanReq);// 发送请求
                try {
                        Objects.requireNonNull(msLoanResp);
                    if (!MsLoanConstant.RetCodeEnum.SUCCESS.VALUE.equals(msLoanResp.getRetStCd())) {// 0：请求成功，其他编码均代表失败
                            logger.error("调用马上审批结果消费接口返回信息" + msLoanResp.getRtnMsg());
                        }
                    } catch (Exception e) {
                        nlsProcessBizVo.setExceptionFlag(true);
                        nlsProcessBizVo.setExceptionMsg(e);
                        logger.error("调用马上审批结果消费接口出现异常：" + e.getMessage());
                        throw new BizException("系统异常", e);
                    }

            } catch (Exception e) {
                nlsProcessBizVo.setExceptionFlag(true);
                nlsProcessBizVo.setExceptionMsg(e);
                logger.error("调用马上审批结果消费接口出现异常：" + e.getMessage());
                throw new BizException("系统异常", e);
            }*/
            StringBuilder resultSb = new StringBuilder();
            if (nlsApplyInfoVO.getRefuseCause().contains(MSRefuseMsgConstant.MsRefuseMsgEnum.REASON_001.getCode()) || nlsApplyInfoVO.getRefuseCause().contains(MSRefuseMsgConstant.MsRefuseMsgEnum.REASON_002.getCode())
                    || nlsApplyInfoVO.getRefuseCause().contains(MSRefuseMsgConstant.MsRefuseMsgEnum.REASON_003.getCode()) || nlsApplyInfoVO.getRefuseCause().contains(MSRefuseMsgConstant.MsRefuseMsgEnum.REASON_004.getCode())
                    || nlsApplyInfoVO.getRefuseCause().contains(MSRefuseMsgConstant.MsRefuseMsgEnum.REASON_005.getCode()) || nlsApplyInfoVO.getRefuseCause().contains(MSRefuseMsgConstant.MsRefuseMsgEnum.REASON_006.getCode())
                    || nlsApplyInfoVO.getRefuseCause().contains(MSRefuseMsgConstant.MsRefuseMsgEnum.REASON_007.getCode())){
                resultSb.append(MSRefuseMsgConstant.RESULT_001);
            }else if (nlsApplyInfoVO.getRefuseCause().contains(MSRefuseMsgConstant.MsRefuseMsgEnum.REASON_010.getCode())){
                resultSb.append(MSRefuseMsgConstant.RESULT_002);
            }else {
                resultSb.append(MSRefuseMsgConstant.RESULT_999);
            }

            nlsProcessBizVo.setRefuseMsg(resultSb.toString());
            throw new BizException(nlsApplyInfoVO.getRefuseCause());
        } catch (Exception e) {
            logger.error("马上贷款审核通知出现异常：" + e.getMessage());
            nlsProcessBizVo.setExceptionFlag(true);
            nlsProcessBizVo.setExceptionMsg(e);
        }
    }
}
